package com.raysweather.android;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class NWSXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static NWSList nwsList = null;
	private int day = 0;

	public static NWSList getSitesList() {
		return nwsList;
	}

	public static void setSitesList(NWSList sitesList) {
		NWSXMLHandler.nwsList = sitesList;
	}

	/** Called when tag starts ( ex:- <name>AndroidPeople</name>
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.contains("nwsWatchesAndWarnings"))
		{
			/** Start */
			nwsList = new NWSList();
		}
		Log.e("NWSXML", localName);

	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name>
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
	throws SAXException {

		currentElement = false;
		/** set value */
		if (localName.equalsIgnoreCase("category"))
			nwsList.setCategory(currentValue);
		else if (localName.equalsIgnoreCase("event"))
			nwsList.setEvent(currentValue);
		else if (localName.equalsIgnoreCase("urgency"))
			nwsList.setUrgency(currentValue);
		else if (localName.equalsIgnoreCase("severity"))
			nwsList.setSeverity(currentValue);
		else if (localName.equalsIgnoreCase("effective"))
			nwsList.setEffective(currentValue);
		else if (localName.equalsIgnoreCase("expires")){
			nwsList.setExpires(currentValue);
		}else if (localName.equalsIgnoreCase("senderName")){
			nwsList.setSenderName(currentValue);
		}else if (localName.equalsIgnoreCase("headline")){
			nwsList.setHeadline(currentValue);
		}else if (localName.equalsIgnoreCase("description")){
			nwsList.setDescription(currentValue);
		}else if (localName.equalsIgnoreCase("instruction")){
			nwsList.setInstruction(currentValue);
		}else if (localName.equalsIgnoreCase("area")){
			nwsList.setArea(currentValue);
		}
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
	throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}
